package com.dotools.encryption;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class EncryptionUtil {
    public Map<String, Object> sortMapByKey(Map map) {
        if (map != null && !map.isEmpty()) {
            TreeMap sortMap = new TreeMap<String, Object>();
            sortMap.putAll(map);
            return sortMap;
        } else {
            return null;
        }
    }

    public String transformationUri(Map<String, Object> map) {
        StringBuilder stringBuilder = new StringBuilder();
        Iterator var2 = map.keySet().iterator();
        while (true) {
            String key;
            do {
                do {
                    if (!var2.hasNext()) {
                        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                        return stringBuilder.toString();
                    }
                    key = (String) var2.next();
                } while ("appSign".equalsIgnoreCase(key));
            } while (map.get(key) == null);

            stringBuilder.append(key);
            stringBuilder.append("=");
            if (!map.get(key).getClass().isArray()) {
                stringBuilder.append(map.get(key));
            } else {
                Object[] objects = (Object[]) map.get(key);
                StringBuilder valueBuilder = new StringBuilder();
                Object[] var6 = objects;
                int var7 = objects.length;
                for (int var8 = 0; var8 < var7; ++var8) {
                    Object o = var6[var8];
                    valueBuilder.append(o);
                    valueBuilder.append(",");
                }
                valueBuilder.deleteCharAt(valueBuilder.length() - 1);
                stringBuilder.append(valueBuilder);
            }
            stringBuilder.append("&");
        }
    }

    /**
     * http://package.api.idotools.com 域名上用此签名方式
     * */
    public String makeSign(String appId, String appSecret, String formatUri) {
        String sha1 = EncryptionDigestUtils.sha1Hex(appSecret + formatUri + appSecret);
        String md5 = EncryptionDigestUtils.md5Hex(sha1 + appId);
        return md5;
    }

    public String sha1Hex(String value) {
        return EncryptionDigestUtils.sha1Hex(value);
    }

    /**screen.api.idotools.com/ 域名下使用 sha1hex 与 md5hex联合的签名方式*/
    public String md5Hex(String value) {
        return EncryptionDigestUtils.md5Hex(value);
    }
}
